﻿--[[
   @brief    view module for repair game
   @author   farleywen
   @version  1.0
   @date     2015-12-18
--]]

repair_game_view =
{
    DLG_FRAME_LENGTH = 195;
    ITEM_LENGTH  = 77;
    DLG_MAX_LENGTH = 494;
    MAX_ITEM_NUM_SHOW = 4;
    
    init = function()
        ierd_tgp.tpf_ui.Log("[repair_game_view] init.");
        ierd_tgp.tpf_ui.Require("tool_box\\repair_game\\model.lua")
        --repair_game_view.open_toolbox_page(uilib);
    end;
    
    close_by_button = function(uilib, dwControlID, x, y)
        base_utility.log("[repair_game_view] close_by_button");
        local parent_dlg_id =  MainCommon.get_parent_id(uilib, dwControlID);
        repair_game_view.close_dlg(uilib, parent_dlg_id, x, y);      
    end;
    
    close_by_gameid = function(game_id) 
        base_utility.log("[repair_game_view] game_id: ".. tostring(game_id));
        if game_id ~= nil and repair_game_model.game2dlg_[game_id] ~= nil then
            local uilib = ui_utility.uilib();
            local dwControlID = repair_game_model.game2dlg_[game_id]:GetControlID();
            repair_game_view.close_dlg(uilib, dwControlID, 0, 0); 
        else
            base_utility.log("[E][repair_game_view] no game_id: ".. tostring(game_id), true);
        end
    end;

    close_dlg = function(uilib, dwControlID, x, y)
        base_utility.log("[repair_game_view] close " .. tostring(dwControlID));
        local game_id = 0;
        for id, dlg in pairs(repair_game_model.game2dlg_) do
            if dlg ~= nil then
                if dlg:GetControlID() == dwControlID then
                    game_id = id;
                    break;
                end
            end
        end
        
        if game_id == 0 then
            base_utility.log("[repair_game_view] close, cannot find game_id", true);
            return;
        end

        repair_game_model.game2dlg_[game_id] = nil;
        local fix_toolbox = repair_game_model.get_fix_toolbox();
        if nil ~= fix_toolbox then
            fix_toolbox:Stop(game_id);
        end

        repair_game_control.set_operate_game_state_for_other_wegame(game_id, false);
        repair_game_control.operate_download_mgr_start_or_stop(true);
        
        local js_id = MainCommon.get_child_id(uilib, dwControlID, "QBWebBrowser_Repair");
        repair_game_model.js2game_[js_id] = nil;
        
        local pControl = uilib:GetControlPtr(dwControlID);
        local pRepairDlg = tolua.cast(pControl, "Tenio::ITDialog");
        --shadow_helper.destroy_shadow(dwControlID);
        pRepairDlg:Show(FALSE);
        layout_mgr.delay_remove_ctrl(dwControlID);
        ierd_tgp.tpf_ui.Log("close repair window success") 
    end;
    
    create = function(game_id, item_num)
        base_utility.log("[repair_game_view] create");
        local dlg_id = layout_mgr.restore_layout_multi("repair_game.tpf");
        local dlg_ctrl = ui_utility.get_ctrl_ptr_by_id(dlg_id);
        if dlg_ctrl ~= NULL then
            local uilib = ui_utility.uilib();
            local js_id = MainCommon.get_child_id(uilib, dlg_id, "QBWebBrowser_Repair");
            repair_game_model.game2dlg_[game_id] = dlg_ctrl;
            repair_game_model.js2game_[js_id] = game_id;
            dlg_ctrl:Show(FALSE);

            repair_game_view.open_toolbox_page(uilib, dlg_id);
            base_utility.log("[repair_game_view] create"..js_id);
            
            --[[
            local title_bar_id = MainCommon.get_child_id(uilib, dlg_id, "TitleBar");
            local title_id = MainCommon.get_child_id(uilib, title_bar_id, "title");
            local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if game_lib ~= nil then
                local game_info = ierd_tgp.game_library.GameDetailInfo:new();
                game_lib:get_game_detail_info(game_id, game_info);
                local game_name = game_info.name;
                game_info:delete();
                local caption_str = game_name.."游戏修复";
                ui_utility.setproperty_by_id(title_id, 'Caption', caption_str);
                ui_utility.setproperty_by_id(dlg_id, 'Caption', caption_str);
            end;
            ]]
        end
    end;
    
    show = function(game_id)
        local pRepairDlg = repair_game_model.game2dlg_[game_id];
        if pRepairDlg  == nil and game_id ~= 0 then
            repair_game_view.create(game_id, 6);
            pRepairDlg = repair_game_model.game2dlg_[game_id];
        end; 
        
        if pRepairDlg ~= nil then
            -- 等待页面数据回来再显示
            --pRepairDlg:Show(TRUE);
            pRepairDlg:Restore();
            pRepairDlg:MoveToCenter();
            pRepairDlg:BringToTop();
        end
    end;
    
    open_toolbox_page = function(uilib, dlg_id)
        local nQBWebID = MainCommon.get_child_id(uilib, dlg_id, "QBWebBrowser_Repair");
        base_utility.log("[open_toolbox_page] id "..nQBWebID);
        local pControl = uilib:GetControlPtr(nQBWebID);
        local pQBWeb = tolua.cast(pControl, "Tenio::IQBWebBrowser");
        local const_ui_str, str_holder = 
        U8toCUIStr(repair_game_model.pageUrl); --临时使用地址
        pQBWeb:Navigate(const_ui_str);

        local url = "ONEIMAGE:tool_box\\game_management_assistant.png;Stretch;0";
        ui_utility.setproperty_by_id(nQBWebID,'Background', url);
        return nQBWebID;
    end;

};

repair_game_view.init();
